//
//  CXBDocument.m
//  CocoaXBI
//

//

#import "CXBDocument.h"

@implementation CXBDocument

@synthesize rootFSElement = _rootFSElement;
@synthesize isoImage = _isoImage;

- (id)init {
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}

- (NSString *)windowNibName {
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"CXBDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController {
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
    
    
    if ( outError != NULL ) {
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
    }
    return NO; // No writing to file for now
}

- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
    
    if (! [absoluteURL isFileURL])
        return NO;
    
    FILE * fileStream = fopen([[absoluteURL path] UTF8String], "r");
    
    XBIError error;
    
    _isoImage = XBIImageCreateFromFile(fileStream, &error);
    
    if (_isoImage != NULL) {
        self.rootFSElement = [[CXBFileSystemElement alloc] initWithXFSElement:XBIImageGetRootFileSystemElement(_isoImage)];
    } else {
        return NO;
    }
    
    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
    }
    return YES;
}

@end
